package com.web;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.entity.ProductBigCategory;
import com.entity.ProductSmallCategory;
import com.service.ProductBigCategoryService;
import com.service.ProductSmallCategoryService;
import com.service.impl.ProductBigCategoryServiceImpl;
import com.service.impl.ProductSmallCategoryServiceImpl;


/**
 * Servlet implementation class ProductSmallCategoryServlet
 */
@WebServlet("/productSmallCategoryServlet")
public class ProductSmallCategoryServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method = request.getParameter("op");
		if(method.equals("queryAllProductBigCategory")){
			queryAllProductBigCategory(request,response);
		}else if(method.equals("addProductSmallCategory")){
			addProductSmallCategory(request,response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	private void queryAllProductBigCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProductBigCategoryService pbs = new ProductBigCategoryServiceImpl();
		List<ProductBigCategory> listProductBigCategory = pbs.queryAllProductBigCategory();
		request.setAttribute("listProductBigCategory", listProductBigCategory);
		request.getRequestDispatcher("/manage/productClass-add.jsp").forward(request, response);
	}
	private void addProductSmallCategory(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ProductSmallCategory productSmallCategory = new ProductSmallCategory();
		String spid = UUID.randomUUID().toString().replace("-", "");
		String spname = request.getParameter("className");
		String sp_bpid = request.getParameter("parentId");
		productSmallCategory.setSpid(spid);
		productSmallCategory.setSpname(spname);
		productSmallCategory.setSp_bpid(sp_bpid);
		ProductSmallCategoryService pbs = new ProductSmallCategoryServiceImpl();
		int recorder = pbs.addProductSmallCategory(productSmallCategory);
		request.getRequestDispatcher("/manage/manage-result.jsp").forward(request, response);
		
	}
}
